/**
 * CharsetDialog.java
 * @author Daniel Liu
 * Create: 2007-1-4 ����03:52:02
 */
package org.lex.swing.widgets;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.charset.Charset;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;

import org.lex.awt.Location;
import org.lex.swing.DocumentAdapter;
import org.lex.swing.Option;


public class CharsetDialog extends EnhancedDialog {
	private JPanel pane = null;
	private JPanel filterPane = null;
	private CharsetPane charsetPane = null;
	private JLabel topLabel = null;
	private JTextField filterTextField = null;
	private JPanel buttonsPane = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JCheckBox filterCheckBox = null;
	// ======================================
	private Option opt = Option.CANCEL;

	public CharsetDialog() {
		super();
		initialize();
	}

	public CharsetDialog(Window owner) {
		super(owner);
		initialize();
	}

	@Override
	public void cancel() {
		opt = Option.CANCEL;
		dispose();
	}

	private void ok() {
		opt = Option.YES;
		dispose();
	}

	Option getSelection() {
		return this.opt;
	}

	Charset[] getSelectedCharsets() {
		return this.getCharsetPane().getSelectedCharsets();
	}

	void setSelectionMode(int mode) {
		this.getCharsetPane().setSelectionMode(mode);
	}

	// =======================
	private void initialize() {
		this.setSize(500, 425);
		this.setTitle("Charset");
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setContentPane(getPane());
		this.setDefaultButton(this.getOkButton());
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(getFilterPane(), BorderLayout.NORTH);
			pane.add(getCharsetPane(), BorderLayout.CENTER);
			pane.add(getButtonsPane(), BorderLayout.SOUTH);
		}
		return pane;
	}

	private JPanel getFilterPane() {
		if (filterPane == null) {
			BorderLayout borderLayout = new BorderLayout();
			borderLayout.setVgap(5);
			borderLayout.setHgap(5);
			topLabel = new JLabel();
			topLabel.setText("All available charsets");
			filterPane = new JPanel();
			filterPane.setLayout(borderLayout);
			filterPane.setBorder(BorderFactory.createEmptyBorder(5, 6, 0, 5));
			filterPane.add(topLabel, BorderLayout.NORTH);
			filterPane.add(getFilterTextField(), BorderLayout.CENTER);
			filterPane.add(getFilterCheckBox(), BorderLayout.WEST);
		}
		return filterPane;
	}

	private JPanel getButtonsPane() {
		if (buttonsPane == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.RIGHT);
			flowLayout.setVgap(6);
			flowLayout.setHgap(5);
			buttonsPane = new JPanel();
			buttonsPane.setLayout(flowLayout);
			buttonsPane.add(getOkButton(), null);
			buttonsPane.add(getCancelButton(), null);
		}
		return buttonsPane;
	}

	private JCheckBox getFilterCheckBox() {
		if (filterCheckBox == null) {
			filterCheckBox = new JCheckBox();
			filterCheckBox.setText("Filter");
			filterCheckBox.addActionListener(new ActionListener() {
				private RowFilter all = new RowFilter() {
					public boolean include(Entry entry) {
						return true;
					}
				};

				public void actionPerformed(ActionEvent e) {
					boolean enabled = filterCheckBox.isSelected();
					getFilterTextField().setEditable(enabled);
					if (false == enabled)
						getCharsetPane().setFilter(all);
					else
						getCharsetPane().setFilter(RowFilter.regexFilter(getFilterTextField().getText()));
				}
			});
			filterCheckBox.setSelected(true);
		}
		return filterCheckBox;
	}

	private JTextField getFilterTextField() {
		if (filterTextField == null) {
			filterTextField = new JTextField();
			filterTextField.getDocument().addDocumentListener(new DocumentAdapter() {
				@Override
				public void documentChanged(DocumentEvent e) {
					String s = filterTextField.getText();
					getCharsetPane().setFilter(RowFilter.regexFilter(s));
				}
			});
		}
		return filterTextField;
	}

	private CharsetPane getCharsetPane() {
		if (charsetPane == null) {
			charsetPane = new CharsetPane();
			charsetPane.setBorder(BorderFactory.createEmptyBorder(5, 6, 0, 5));
		}
		return charsetPane;
	}

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ok();
				}
			});
		}
		return okButton;
	}

	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					cancel();
				}
			});
		}
		return cancelButton;
	}

	public static void main(String[] args) {
		JDialog dialog = new CharsetDialog();
		Location.moveToScreenCenter(dialog);
		dialog.setVisible(true);
	}
}
